<template>
  <Viewer :router="viewerRouter"></Viewer>
</template>

<script setup>
import { defineAsyncComponent, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useForyouStore } from '@/store/home/foryou'
import { useNavRefresh } from '@/compositions/nav/refresh'
import { useInfiniteForyou } from '@/compositions/request/infiniteForyou'
import { useDesktopViewerRouter } from '@/compositions/viewer/router/desktopViewerRouter'
import { NAVS } from '@/constant'

const Viewer = defineAsyncComponent(() => import('@/components/viewer/Viewer.vue'))

const viewerRouter = useDesktopViewerRouter('desktop-home-foryou')
const { currIndex, setFromMainViewer, backToFirst } = viewerRouter

const foryouStore = useForyouStore()
const { shareAff } = storeToRefs(foryouStore)

function setupViewer() {
  if (currIndex.value > 0) {
    backToFirst()
  } else {
    setFromMainViewer({ foryou: useInfiniteForyou({ params: { share_aff: shareAff.value } }) })
  }
}

onMounted(setupViewer)

// nav 點擊原本頁面刷新
useNavRefresh(NAVS.HOME, setupViewer)
</script>
